home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / class / file.d < prev    next >
Text File  |  1996-02-04  |  3KB  |  174 lines

  1.  
  2. /*
  3.  *
  4.  *    Copyright (c) 1993-1996 Algorithms Corporation
  5.  *    3020 Liberty Hills Drive
  6.  *    Franklin, TN  37067
  7.  *
  8.  *    ALL RIGHTS RESERVED.
  9.  *
  10.  *
  11.  *
  12.  */
  13.  
  14.  
  15.  
  16. #include <sys/types.h>
  17. #if    !defined(vms)  &&  !defined(__MWERKS__)
  18. #include <sys/stat.h>
  19. #endif
  20. #ifdef    sparc
  21. #include <unistd.h>
  22. #endif
  23.  
  24.  
  25. defclass  File : Stream  {
  26.     iName;
  27.     FILE    *iFP;
  28.  init:    class_init;
  29. };
  30.  
  31. cmeth    gNew()
  32. {
  33.     return gShouldNotImplement(self, "gNew");
  34. }
  35.  
  36. cmeth    gOpenFile, <vNew> (char *name, char *mode)
  37. {
  38.     object    obj;
  39.     ivType    *iv;
  40.     FILE    *fp;
  41.  
  42.     if (IsObj((object) name))
  43.         name = gStringValue((object) name);
  44.     if (IsObj((object) mode))
  45.         mode = gStringValue((object) mode);
  46.     fp = fopen(name, mode);
  47.     if (!fp)
  48.         return NULL;
  49.     obj = gNew(super);
  50.     iv = ivPtr(obj);
  51.     iName = gNewWithStr(String, name);
  52.     iFP = fp;
  53.     return obj;
  54. }
  55.  
  56. private    cmeth    StreamObject(object self, char *name, FILE *fp)
  57. {
  58.     object    obj;
  59.     ivType    *iv;
  60.  
  61.     obj = gNew(super);
  62.     iv = ivPtr(obj);
  63.     iName = gNewWithStr(String, name);
  64.     iFP = fp;
  65.     return obj;
  66. }
  67.  
  68. imeth    object    gDispose, gDeepDispose, gGCDispose ()
  69. {
  70.     fclose(iFP);
  71.     gDispose(iName);
  72.     gDispose(super);
  73.     return NULL;
  74. }
  75.  
  76. imeth    int    gRead(char *buf, unsigned n)
  77. {
  78.     return fread(buf, 1, n, iFP);
  79. }
  80.  
  81. imeth    int    gWrite(char *buf, unsigned n)
  82. {
  83.     return fwrite(buf, 1, n, iFP);
  84. }
  85.  
  86. imeth    char    *gGets(char *buf, int sz)
  87. {
  88. /*    char    *fgets(char *, int, FILE *);  */
  89.     return fgets(buf, sz, iFP);
  90. }
  91.  
  92. imeth    long    gAdvance(long n)
  93. {
  94.     int    r = fseek(iFP, n, SEEK_CUR);
  95.     return r ? 0L : n;
  96. }
  97.  
  98. imeth    long    gRetreat(long n)
  99. {
  100.     int    r = fseek(iFP, -n, SEEK_CUR);
  101.     return r ? 0L : n;
  102. }
  103.  
  104. imeth    long    gSeek(long n)
  105. {
  106.     int    r = fseek(iFP, n, SEEK_SET);
  107.     return r ? 0L : n;
  108. }
  109.  
  110. imeth    long    gPosition()
  111. {
  112. /*    long    ftell(FILE *);  */
  113.     return ftell(iFP);
  114. }
  115.  
  116. imeth    long    gLength()
  117. {
  118. #if    !defined(vms)  &&  !defined(__MWERKS__)
  119.     struct    stat    sb;
  120.     int    r;
  121.  
  122.     r = fstat(fileno(iFP), &sb);
  123.     return r ? -1L : sb.st_size;
  124. #else
  125.     long    sav = ftell(iFP);
  126.     long    len;
  127.  
  128.     fseek(iFP, 0L, SEEK_END);
  129.     len = ftell(iFP);
  130.     fseek(iFP, sav, SEEK_SET);
  131.     return len;
  132. #endif
  133. }
  134.  
  135. imeth    char    *gName()
  136. {
  137.     return gStringValue(iName);
  138. }
  139.  
  140. imeth    int    gEndOfStream()
  141. {
  142.     return feof(iFP);
  143. }
  144.  
  145. imeth    void    *gPointerValue()
  146. {
  147.     return (void *) iFP;
  148. }
  149.  
  150. static    void    class_init(void)
  151. {
  152.     String;  /*  must be initialized!  */
  153.     stdoutStream_o = StreamObject(CLASS, "stdout", stdout);
  154.     stderrStream_o = StreamObject(CLASS, "stderr", stderr);
  155.     stdinStream_o  = StreamObject(CLASS, "stdin", stdin);
  156.     traceStream_o  = stdoutStream_o;
  157. }
  158.  
  159.  
  160.  
  161.  
  162. /*
  163.  *
  164.  *    Copyright (c) 1993-1996 Algorithms Corporation
  165.  *    3020 Liberty Hills Drive
  166.  *    Franklin, TN  37067
  167.  *
  168.  *    ALL RIGHTS RESERVED.
  169.  *
  170.  *
  171.  *
  172.  */
  173.  
  174.